var utils = require('/utils/index.js');
var base = require('/base_info.js');
Page({
  data: {
    inTime: '',
    outTime: '',
    lightLong: 0,
    roomList: [],
    imgUrl: base.imgUrl,
    recommend: '',
    showIndex: ''
  },
  // 展开和显示协议价，会员价
  showMenberPrice (e) {
    if (this.data.showIndex === '' || this.data.showIndex != e.currentTarget.dataset.id) {
      this.setData({
        showIndex: e.currentTarget.dataset.id
      });
      return false;
    };
    this.setData({
      showIndex: ''
    })
  },
  getData(recommend) {
    my.showLoading();
    let that = this;
    utils.getData('SingleRoomList', {
      seller_id: base.seller_id,
      recommend: recommend == '1' ? 1 : "",
      start: my.getStorageSync({
        key: 'inTime',
      }).data,
      end: my.getStorageSync({
        key: 'outTime',
      }).data,
      // uid: 196
      uid: that.data.uid
    }, (res) => {
      my.hideLoading();
      console.log(res.data.data);
      this.setData({
        roomList: res.data.data
      });
    }, (err) => {
      my.hideLoading();
      console.log(err);
    });
  },
  onLoad(query) {
    // 页面加载
    this.setData({
      recommend: query.recommend
    })
  },
  detail (e) {
    let that = this;
    if (e.currentTarget.dataset.sign === '') {
      return false;
    }
    console.log();
    my.navigateTo({
      url: './room-detail?id=' + e.currentTarget.dataset.id + '&type=' + e.currentTarget.dataset.sign
    });
  },
  // 跳转到时间选择页面
  timeSelect () {
    my.navigateTo({
      url: './../time/time-native'
    });
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
    let that = this;
    // 获取缓存时间
    let inTime = my.getStorageSync({
      key: 'inTime',
    }).data;
    let outTime = my.getStorageSync({
      key: 'outTime',
    }).data;
    let lightLong = (new Date(outTime)) - (new Date(inTime));
    console.log(lightLong);
    try {
      this.setData({
        inTime: my.getStorageSync({
          key: 'inTime',
        }).data.slice(5, 10),
        outTime: my.getStorageSync({
          key: 'outTime',
        }).data.slice(5, 10),
        lightLong: lightLong / 86400000
      });
    } catch (err) {
      this.setData({
        inTime: '',
        outTime: '',
        lightLong: 0
      });
    }
    var uid = my.getStorageSync({
      key: 'uid', // 缓存数据的key
    }).data;
    console.log(my.getStorageSync({
      key: 'uid', // 缓存数据的key
    }).data);
    console.log(!uid || uid == "");
    if (!uid || uid == "") {
      utils.getAuthCode(function(res){
        console.log(res.data);
        that.setData({
          uid: res.data.data.uid
        })
        that.getData(that.data.recommend);
      });
      return false;
    };
    that.setData({
      uid: uid
    })
    this.getData(that.data.recommend);
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
